<?php

if(stristr($_SERVER['PHP_SELF'],".inc.php")) { 
	header("Location:index.php"); 
	exit; 
}

if($_POST['op'] == "e" && isset($_pw)) {
        $id = intval($_POST['u']);
		$title = $_POST['title'];
		$teaser = $_POST['teaser'];
		$post = $_POST['post'];
		$topic = intval($_POST['topic']);
		$public = intval($_POST['public']);
		if(!$_POST['pubdate']) { $_POST['pubdate']= date("m-d-Y H:i:s"); } else { $pubdate=$_POST['pubdate']; }
		$pubdate = parse_pubdate($_POST['pubdate'],$CONFIG['backend']);		
		change_post($id,$pubdate,$title,$teaser,$post,$public,$topic); 
		unset($goto); 
		$goto = $self."?m=e&id=".$id; 
		header("Location: ".$goto);
		exit; 
} else { 
 	$_hideForm = TRUE;
	$_hidePosts = TRUE; 
	$id = intval($_REQUEST['id']);
	$the = get_post($id); 
	if($_GET['m'] == "X") { 
		//reset some vars
		$the['title'] 	= urldecode($_GET['title']);
		$the['post']	= urldecode($_GET['post']);
	}	
	$topic = get_topic($the['topic']);
	$tags = get_tags($id);
?>

	<form action="<?php echo $self; ?>" method="post" name="myform">
	<input type="hidden" name="op" value="e" />
	<input type="hidden" name="u" value="<?php echo $id;?>" />	
	<div id="add_item">
		<div id="add_item_header"><?php echo ucwords($Lang['edit'] . " " . $Lang['post']); ?></div>
	
	<div id="add_item_title">
		<input class="item_title" type="text" value="<?=htmlspecialchars($the['title']);;?>" name="title" maxlength="100" />
	</div>

	<fieldset id="add_item_teaser">
		<legend><?=$Lang['teaser'];?></legend>
		<textarea name="teaser" id="teaser"><?= htmlspecialchars(stripslashes(str_replace("<br />","\n",$the['teaser']))); ?></textarea>
		<br />
		<input type="button" class="button" value="Bold" name="b" onclick="boldify2('teaser')">&nbsp;
		<input type="button" class="button" value="Italic" name="i" onclick="italify2('teaser')">&nbsp;
		<input type="button" class="button" value="Underline" name="u" onclick="underlinify2('teaser')">&nbsp;
		<input type="button" class="button" value="Insert Link" name="href" onclick="linkify2('teaser')">&nbsp;
		<input type="button" class="button" value="Insert Image" name="img" onclick="imagify2('teaser')">
	</fieldset>

<?php if(strlen($the['post']) > 2) { $aibc="none"; $aib="block"; }  else { $aibc="block";$aib="none"; } ?>	
	<div id="add_item_body_collapsed" style="display:<?=$aibc;?>;">
		<button onclick="javascript:toggle_body()" type="button" class="button">Show Body</button>
	</div>
	<fieldset id="add_item_body" style="display:<?=$aib;?>">
		<legend><?=$Lang['post'];?></legend>
		<textarea name="post" id="post" cols="50" rows="10"><?= htmlspecialchars(stripslashes(str_replace("<br />","\n",$the['post']))); ?></textarea>
		<br/><input type="button" class="button" value="Bold" name="b" onclick="boldify2('post')">&nbsp;
		<input type="button" class="button" value="Italic" name="i" onclick="italify2('post')">&nbsp;
		<input type="button" class="button" value="Underline" name="u" onclick="underlinify2('post')">&nbsp;
		<input type="button" class="button" value="Insert Link" name="href" onclick="linkify2('post')">&nbsp;
		<input type="button" class="button" value="Insert Image" name="img" onclick="imagify2('post')">
		<br /><br />
		<button onclick="javascript:toggle_body()" type="button" class="button">Hide Body</button>		
	</fieldset>	

	<fieldset id="metadata">
		<legend><?=ucwords($Lang['metadata']);?></legend>
		<?php echo ucwords($Lang['topic']); ?>:  
			<select name="topic" id="topic">
<?php
	foreach($topics as $k=>$v) { 
		echo "\t\t\t<option value=\"".$v['topicid']."\"";
		if($the['topic']==$v['topicid']) { echo "selected=\"selected\""; }
		echo ">".$v['name']."</option>\n";
	}
?>	
	</select> [<a href="javascript:topicUpdate(<?=$id;?>);">Update Now</a>]
	<br /><br />
	
	<?php echo $Lang['tags']; ?>: <span id="tags">
<?php
if(is_array($tags)) { 
	foreach($tags as $key=>$val) { 
		echo "<div id=\"tagspan_".$key."\"><input type=\"text\" name=\"tag_".$key
		. "\" id=\"tag_".$key."\" value=\"".$val."\" /> "
		. " [<a href=\"javascript:tagUpdate('".$key."');\">".$Lang['edit']."</a>]"
		. " [<a href=\"javascript:tagDelete('".$key."')\">".$Lang['delete']."</a>]</span></div>";
	}
}
?>	
	[<a href="javascript:tagAdd('<?=$id;?>')">Add new tag</a>]<span>
	</fieldset>
	
	<fieldset id="add_item_pubdate">
		<legend><?php echo ucwords($Lang['pubdate']); ?></legend>
		<input id="pubdate" name="pubdate" type="text" value="<?=$the['pubdate']; ?>" size="25">
		<a href="javascript:NewCal('pubdate','YYYYMMDD',true)">
		<img src="./inc/datetime/cal.gif" width="16" height="16" border="0" alt="Pick a date" /></a>
	</fieldset>
		
	<fieldset id="add_item_public">
		<legend><?php echo ucwords($Lang['public']); ?></legend>
		<input type="radio" name="public" value="1" checked="checked"><?php echo $Lang['unhideThisPost']; ?>
		<input type="radio" name="public" value="0"><?php echo $Lang['hideThisPost']; ?>
		</select>
	</fieldset>

	<div id="add_item_buttons">
		<input type="submit" name="add" class="button" value="<?php echo $Lang['edit'] . " " . $Lang['post']; ?>" />
	</div>

	</div>
	</form> 
	<?php } ?>
